<?php

namespace Home\Controller;
use Think\Controller;
class ForgetPassController extends Controller {

    public function ForgetPassword(){
        $logo=M('logo')->field('id,logo,ico,crm_logo')->find();
        $this->assign('logo',$logo);
        $this->display();
    }

    //发送验证码 手机||邮箱
    public function SendCode(){
        $data = I('post.email') ? I('post.email') : die(json_encode(array('msg'=>'非法操作','status'=>0)));
        $phone = "/^1[34578]{1}\d{9}$/";
        $result = filter_var($data, FILTER_VALIDATE_EMAIL);
        $type = 1;
        if($result!=false){
            $user = M('user')->where(['user_email'=>$data])->find();
            $type = 1;
        }
        if(preg_match($phone,$data)){
            $user = M('user')->where(['user_phone'=>$data])->find();
            $type = 2;
        }
        if(!empty($user)){
            $send['code']   = rand(pow(10,(6-1)), pow(10,6)-1);//验证码生成
            $send['time']   = time();
            $send['mail']   = $data;
            $send['mt4']    = $user['user_id'];
            $add  = M('forget_pwd_code')->add($send);//验证码写入数据库
            if($add){
                if($type==1){//邮箱发送
                    $mail_mould = M('mail_mould')->where(['type'=>10010])->find();//查询邮件模板
                    $msg = str_replace('{1}',$send['code'],$mail_mould['content']);
                    $Send = SendMail($data,$mail_mould['title'],htmlspecialchars_decode($msg));//发送
                }else{
                    $Send =1;   //手机发送
                }
                if($Send==1){
                    $this->ajaxReturn(array('msg'=>'验证码发送成功','status'=>1));
                }else{
                    $this->ajaxReturn(array('msg'=>'验证码发送失败','status'=>0));
                }
            }else{
                $this->ajaxReturn(array('msg'=>'发送失败请重试','status'=>0));
            }
        }else{
            $this->ajaxReturn(array('msg'=>'您填写的账号未注册','status'=>0));
        }
    }

    public function resetPwd(){
        $email=I('post.user_email') ? I('post.user_email') : die(json_encode(array('msg'=>'请输入账号','status'=>0)));
        $code=I('post.code') ? I('post.code') : die(json_encode(array('msg'=>'请输入验证码','status'=>0)));
        $pwd=I('post.password') ? I('post.password') : die(json_encode(array('msg'=>'请输入新的密码','status'=>0)));
        $repwd=I('post.repwd') ? I('post.repwd') : die(json_encode(array('msg'=>'请确认密码','status'=>0)));
        if($pwd != $repwd){
            $this->ajaxReturn(array('msg'=>'确认密码不一致','status'=>0));
        }
        $msg=M('forget_pwd_code')->where(['mail'=>$email,'code'=>$code])->find();
        if(empty($msg)){
            $this->ajaxReturn(array('msg'=>'账号或验证码填写错误','status'=>0));
        }else{
            $time=time()+600;
            if($msg['time']>$time){
                $this->ajaxReturn(array('msg'=>'验证码过期，请重新获取','status'=>0));
            }
            $data['user_password']=md5(md5($pwd));
            $res=M('user')->where(['user_email'=>$email])->save($data);
            if($res!==false){
                $this->ajaxReturn(array('msg'=>'操作成功','status'=>1));
            }else{
                $this->ajaxReturn(array('msg'=>'操作失败','status'=>0));
            }
        }
    }



}